﻿namespace ClassyBlog.DataAccess
{
    using System.IO;

    using DomainObjects;
    using Infrastructure;

    public class PageFactory : ArticleFactory<Page>
    {
        public PageFactory(IFileSystem fileSystem) : base(fileSystem)
        {
        }

        protected override Page CreateLazy(string dataPath, string filePath)
        {
            var path = Path.GetDirectoryName(filePath);

            Category category = null;

            if (!string.IsNullOrWhiteSpace(path) &&
                !string.IsNullOrWhiteSpace(dataPath))
            {
                var categorySegment = path.Substring(dataPath.Length);

                category = Category.Parse(categorySegment);
            }

            var loader = new LazyArticleLoader<Page>(filePath, FileSystem);
            var page = new LazyPage(loader) { Category = category };

            return page;
        }
    }
}